// packageA/pages/coupon-list/index.js
const WXAPI = require("../../api/goods");
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ProductGoods: [],
    firstInfo: "",
    secondInfo: "",
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    pageSize: 15,
    pageNum: 1,
    active: "1",
    isLoading: false,
    listIsOver: false,
    total: 0,
    currentPageNum: 1,
    couponList: [],
    isClick: false,
  },
  //上拉触底
  onScrollBottom() {
    // if (this.data.pageNum < this.data.currentPageNum) {
    //   this.setData({
    //     pageNum: this.data.pageNum + 1,
    //   });
    //   this.getCouponList();
    // } else if (
    //   this.data.pageNum > this.data.currentPageNum ||
    //   this.data.pageNum == this.data.currentPageNum
    // ) {
    //   this.setData({
    //     listIsOver: true,
    //   });
    // }
  },
  //下拉刷新
  async onRefresh() {
    this.setData({
      pageNum: 1,
      couponList: [],
    });
    this.getCouponList();
  },

  onChange(e) {
    this.setData({
      active: e.detail.name,
    });
    this.onRefresh();
  },
  // noYH() {
  //   wx.navigateBack({
  //     success: function () {
  //       var page = getCurrentPages();
  //       var prevPage = page[page.length - 2];
  //       // 存储上一页数据
  //       prevPage.setData({
  //         couponId: "",
  //       });
  //       // 返回刷新
  //       if (prevPage == undefined || prevPage == null) return;
  //       prevPage.onLoad();
  //     },
  //   });
  // },
  sure() {
    let arr = [];
    for (let i = 0; i < this.data.couponList.length; i++) {
      for (let j = 0; j < this.data.couponList[i].OrgDiscounts.length; j++) {
        if (this.data.couponList[i].OrgDiscounts[j].selected) {
          arr.push(this.data.couponList[i].OrgDiscounts[j].sId);
        }
      }
    }
    var page = getCurrentPages();
    var prevPage = page[page.length - 2];
    // 存储上一页数据
    prevPage.setData({
      couponId: arr,
      couponList: this.data.couponList,
    });
    // prevPage.onLoad();
    wx.navigateBack({
      detail: 1,
    });
  },
  selectCoupon(e) {
    if (this.data.active == 0 || !this.data.isClick) {
      return;
    }
    let arr = this.data.couponList;
    const { id, selected, orgid } = e.currentTarget.dataset;
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < arr[i].OrgDiscounts.length; j++) {
        if (orgid == arr[i].sOrgId) {
          if (id == arr[i].OrgDiscounts[j].sId) {
            if (selected) {
              arr[i].OrgDiscounts[j].selected = false;
            } else {
              arr[i].OrgDiscounts[j].selected = true;
            }
          } else {
            arr[i].OrgDiscounts[j].selected = false;
          }
        }
      }
    }
    this.setData({
      couponList: arr,
    });
  },
  async getCouponList(val) {
    this.setData({
      listIsOver: false,
      isLoading: true,
    });
    let data = {
      // PageIndex: this.data.pageNum,
      // PageSize: this.data.pageSize,
      ProductGoods: this.data.ProductGoods,
      iPublish: 1,
      iHasUseDiscount: val ? val : this.data.active,
    };
    let isCustomLoading = true;
    let listArr = [];
    let res = await WXAPI.getCouponPageList(data, isCustomLoading);
    if (res.data.code == 0) {
      if (data.iHasUseDiscount == "0") {
        this.setData({
          secondInfo: res.data.data.total,
        });
      }
      if (data.iHasUseDiscount == "1") {
        this.setData({
          firstInfo: res.data.data.total,
        });
      }
      if (this.data.pageNum == 1) {
        listArr = res.data.data.OrgDiscountGroup;
      } else {
        listArr = this.data.couponList;
        Array.prototype.push.apply(listArr, res.data.data.OrgDiscountGroup);
      }
      this.setData({
        listIsOver: true,
      });
      let objImages = {
        0: "appletstaticimg/coupon-none.png",
        1: "appletstaticimg/coupon-1.png",
        2: "appletstaticimg/coupon-2.png",
        3: "appletstaticimg/coupon-3.png",
      };
      for (let i = 0; i < listArr.length; i++) {
        for (let j = 0; j < listArr[i].OrgDiscounts.length; j++) {
          if (this.data.active == "1") {
            for (let k = 1; k < 3; k++) {
              listArr[i].OrgDiscounts[j].currentImageName = objImages[k];
            }
          }
          if (this.data.active == "0") {
            listArr[i].OrgDiscounts[j].currentImageName = objImages[0];
          }
        }
      }
      this.setData({
        total: res.data.data.total,
        currentPageNum: res.data.data.OrgDiscountGroup.length,
        isLoading: false,
      });
      console.log("val", val);
      if (val) {
        this.setData({
          couponList: val == this.data.active ? listArr : [],
        });
      } else {
        this.setData({
          couponList: listArr,
        });
      }
      console.log("couponList", this.data.couponList);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    if (options.ProductGoods) {
      this.setData({
        ProductGoods: JSON.parse(options.ProductGoods),
      });
    }
    if (options.isClick == 1) {
      this.setData({
        isClick: true,
      });
    }
    await this.getCouponList("0");
    if (options.couponList && JSON.parse(options.couponList).length > 0) {
      this.setData({
        couponList: JSON.parse(options.couponList),
      });
      return false;
    }
    await this.getCouponList("1");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
